package org.example.back.controller.health;

import org.example.back.entity.health.ChronicDisease;
import org.example.back.service.health.ChronicDiseaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpSession;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/health/chronic-disease")
public class ChronicDiseaseController {

    @Autowired
    private ChronicDiseaseService chronicDiseaseService;

    // 添加慢性病记录
    @PostMapping("/add")
    public Map<String, Object> addChronicDisease(@RequestBody ChronicDisease chronicDisease, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }

        // 设置用户ID
        chronicDisease.setUserId(userId);

        boolean success = chronicDiseaseService.saveChronicDisease(chronicDisease);
        if (success) {
            return Map.of("success", true, "message", "慢性病记录添加成功");
        } else {
            return Map.of("success", false, "message", "慢性病记录添加失败");
        }
    }

    // 获取当前用户的所有慢性病记录
    @GetMapping("/list")
    public Map<String, Object> getChronicDiseases(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }

        List<ChronicDisease> diseases = chronicDiseaseService.getChronicDiseasesByUserId(userId);
        return Map.of("success", true, "data", diseases);
    }

    // 获取特定慢性病记录详情
    @GetMapping("/detail/{id}")
    public Map<String, Object> getChronicDiseaseDetail(@PathVariable Integer id, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }

        ChronicDisease disease = chronicDiseaseService.getChronicDiseaseById(id);

        if (disease == null) {
            return Map.of("success", false, "message", "记录不存在");
        }

        // 验证记录所有者
        if (!disease.getUserId().equals(userId)) {
            return Map.of("success", false, "message", "无权访问该记录");
        }

        return Map.of("success", true, "data", disease);
    }

    // 更新慢性病记录
    @PutMapping("/update")
    public Map<String, Object> updateChronicDisease(@RequestBody ChronicDisease chronicDisease, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }

        // 验证记录所有者
        ChronicDisease existingDisease = chronicDiseaseService.getChronicDiseaseById(chronicDisease.getId());
        if (existingDisease == null) {
            return Map.of("success", false, "message", "记录不存在");
        }

        if (!existingDisease.getUserId().equals(userId)) {
            return Map.of("success", false, "message", "无权修改该记录");
        }

        // 保持用户ID不变
        chronicDisease.setUserId(userId);

        boolean success = chronicDiseaseService.updateChronicDisease(chronicDisease);
        if (success) {
            return Map.of("success", true, "message", "慢性病记录更新成功");
        } else {
            return Map.of("success", false, "message", "慢性病记录更新失败");
        }
    }

    // 删除慢性病记录
    @DeleteMapping("/delete/{id}")
    public Map<String, Object> deleteChronicDisease(@PathVariable Integer id, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }

        // 验证记录所有者
        ChronicDisease existingDisease = chronicDiseaseService.getChronicDiseaseById(id);
        if (existingDisease == null) {
            return Map.of("success", false, "message", "记录不存在");
        }

        if (!existingDisease.getUserId().equals(userId)) {
            return Map.of("success", false, "message", "无权删除该记录");
        }

        boolean success = chronicDiseaseService.deleteChronicDisease(id);
        if (success) {
            return Map.of("success", true, "message", "慢性病记录删除成功");
        } else {
            return Map.of("success", false, "message", "慢性病记录删除失败");
        }
    }
}